<%@ page pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="isperp" uri="/WEB-INF/tlds/isperp.tld"%>

<div class="header">
	<h2>
		绩效考核信息<span><font color="red">（* 为必填项）</font> </span>
	</h2>
</div>
<script type="text/javascript">
	var containUnscore = null;
	function cusAction(rowObject) {
		var sourceId = $("#id").val();
		var id = rowObject.id;
		return "<a class='nui-txt-link' onclick=addPreItemDetail('" + id + "')>修改</a>";
	}

	function addPreItemDetail(id) {
		isperp.openDialog({
			id : "addPreItemDetailDialog",
			windowSize : "normal",
			contentUrl : 'performance/performanceItemDetail/edit?pcontrolid=preItemDetailGrid&id=' + id  + '&type=0',
			title : "新增"
		});
	}
	function checkScore(cellvalue) {
		containUnscore = false;
		if (!cellvalue) {
			containUnscore = true;
			return "<span style='color:red'>未评价</span>";

		} else
			return cellvalue;
	}
	function LoadScore(data) {
		//getSelfTotalScore();
	}
	function formatText(v) {
		if (v != null && v != "")
			return "<pre>" + v + "</pre>";
		else
			return "";
	}

	function updateValue(obj, columnName, id, type) {
		var value = obj.value;
		var perUserSubmitId = $("#id").val();
		var selfScore = $("#self_score_" + id).val();
		if (type == 0) {
			if (/^(\+|-)?\d+($|\.\d+$)/.test(selfScore)) {
				var tsc = parseFloat(selfScore);
				if (tsc<0||tsc>150) {
					isperp.alert("请输入150分内的分值！");
					return false;
				}
			} else {
				isperp.alert("请输入数字！");
				return false;
			}
		}

		var situation = $("#situation_" + id).val();
		$.ajax({
			url : 'performance/performanceItemDetail/updateValue',
			data : {
				"id" : id,
				"selfScore" : selfScore,
				"situation" : situation,
				"perUserSubmitId" : perUserSubmitId
			},
			success : function(data) {
				//preItemDetailGrid.search();
				getSelfTotalScore();
				/*ifReload = true;
				 $("[id^='self_score_']").each(function() {
					if ($(this).val() == "") {
						ifReload = false;
					}
				});
				if (ifReload) {
					preItemDetailGrid.search();
				} */
			}
		});
	}

	function updateText1(v, o, r, c) {
		containUnscore = false;
		var cellId = r.id;
		var weight = r.weight;
		if (v != null)
			return "<input id='" + c + "_" + cellId + "' class='perInput number' data-weight='" + weight + "' value='" + v + "' onchange=\"updateValue(this,'" + c + "','" + cellId + "',0)\"/>";
		else
			return "<input id='" + c + "_" + cellId + "' class='perInput number' data-weight='" + weight + "' onchange=\"updateValue(this,'" + c + "','" + cellId + "',0)\"/>";
	}
	function updateText2(v, o, r, c) {
		var cellId = r.id;
		if (v != null && v != "")
			//return "<pre>" + v + "</pre>";
			return "<textarea id='" + c + "_" + cellId + "' class='perInput1' onchange=\"updateValue(this,'" + c + "','" + cellId + "',1)\">" + v + "</textarea>";
		else
			return "<textarea id='" + c + "_" + cellId + "' class='perInput1' onchange=\"updateValue(this,'" + c + "','" + cellId + "',1)\"></textarea>";
	}
	function updateText3(v) {
		return v + "%";
	}
</script>
<style>
.ui-jqgrid-view .ui-jqgrid-bdiv td {
	white-space: nowrap;
	text-indent: 6px;
	text-overflow: initial;
	overflow: inherit;
	padding: 0px;
	font-size: 11px;
}

.perInput {
	height: 98%;
	width: 50%;
}

.perInput1 {
	height: 98%;
	width: 70%;
}

.textareaInput {
	white-space: normal;
	text-align: justify;
	-moz-text-align-last: center; /* Firefox 12+ */
	text-align-last: center;
}

.ui-jqgrid-bdiv {
	overflow: hidden;
}
</style>
<form:form commandName="model" cssClass="validate" id="performanceForm"
	action="performance/performance/save">
	<form:hidden path="id" />
	<form:hidden path="selfSubmitDate" />
	<div class="box" style="overflow: hidden">
		<isperp:processProcesser processId="${model.processId }"></isperp:processProcesser>
	</div>
	<table class=" form-table">
		<tbody>

			<tr>
				<td class="td1">被考核人：</td>
				<td class="td2"><form:input path="perUserFullname"
						style="display:none" /> ${model.perUserFullname }</td>

				<td class="td1">岗位：</td>
				<td class="td2"><form:input path="positionName"
						style="display:none" /> ${model.positionName }</td>
			</tr>

			<tr>
				<td class="td1">选择模板：</td>
				<td class="td2" colspan="3"><form:select path="perSettingId"
						cssClass="required" onchange="chosePerSetting()" cssStyle="width:80%" data-width="300px"
						data-allow-empty="true" items="${persetList}" itemLabel="name"
						itemValue="id" /></td>
			</tr>

			<tr>
				<td class="td1">考核时间：</td>
				<td class="td2">
					<div id="dateSelector" style="display:inline-block;">
						<select id="idYear" name="perYear" data="" data-width="70px"
							data-allow-empty="false" data-width="70px"></select> 年 <select
							data-width="60px" id="idMonth" name="perMonth" data=""
							data-width="60px" data-allow-empty="false"></select> 月
					</div>
				</td>

				<td class="td1">审核人：</td>
				<td class="td2"><form:hidden path="managerName" /> <isperp:departmentUser
						displayValue="${model.managerFullname }"
						parentIdControlId="8135359050480003" valueControlId="managerName"
						idFieldName="name" id="managerFullname"
						ifIncludeNoRoleUser="false" name="managerFullname"
						multiselect="false" displayValueCssClass="required"></isperp:departmentUser>
				</td>
			</tr>

		</tbody>
	</table>
</form:form>
<div class="header">
	<h2>
		指标</span>
	</h2>
</div>
<isperp:grid
	url="performance/performanceItemDetail/listData?sourceId=${model.id }"
	gridId="preItemDetailGrid" showPager="false" showSearchbar="false" 
	showToolbar="false" cusDialogSize="big" loadComplete="LoadScore(data);"
	 cusFormatterActions="false">
	<jsp:body>
		<isperp:column name="id" title="id" hidden="true"></isperp:column>
	    <isperp:column name="name" title="指标名称"
			formatter="return formatText(cellvalue)" isFormatterFun="true"
			width="70"></isperp:column>
	    <isperp:column name="target" title="目标"
			formatter="return formatText(cellvalue)" isFormatterFun="true"
			width="150"></isperp:column>
	    <isperp:column name="referenc_point" title="衡量标准"
			formatter="return formatText(cellvalue)" isFormatterFun="true"
			width="150"></isperp:column>
	    <isperp:column name="weight" title="权重" width="40"
			formatter="return updateText3(cellvalue)" isFormatterFun="true"></isperp:column>
	</jsp:body>
</isperp:grid>

<isperp:buttonList>
	<isperp:processButtons processKey="performanceProcess"
		processId="${model.processId }" businessKey="${model.id }"
		formId="performanceForm"></isperp:processButtons>
</isperp:buttonList>
<script type="text/javascript">
	function chosePerSetting() {
		var o = '${model.perSettingId}';
		var id = $("#id").val();
		var perSettingId = $("#perSettingId").val();
		if (o && perSettingId) {
			if (confirm("改变模板将需要重新填写指标值，是否继续？")) {
				$.ajax({
					url : "performance/performanceItemDetail/insertPerSettingItem?perSettingId="+perSettingId+"&perUserSubmitId="+id,
					success : function(data) {
						preItemDetailGrid.search();
					}
				});
			} else {
				$("#perSettingId").val(o);
				$("#perSettingId").trigger("chosen:updated");
			}
		} else {
			$.ajax({
				url : "performance/performanceItemDetail/insertPerSettingItem?perSettingId="+perSettingId+"&perUserSubmitId="+id,
				success : function(data) {
					preItemDetailGrid.search();
				}
			});
		}
	}

	var myDate = new Date();
	var curMonth=myDate.getMonth() + 1;
	var curDate=myDate.getDate();
	if(curDate<15)
	  curMonth=myDate.getMonth();
	$("#dateSelector").DateSelector({
		ctlYearId : 'idYear',
		ctlMonthId : 'idMonth',
		defYear : myDate.getFullYear(),
		defMonth : curMonth,
		defDay : 1,
		minYear : 2016,
		maxYear : (myDate.getFullYear() + 1)
	});

	function getSelfTotalScore() {
		var totalScore = 0;
		$("[id^='self_score_']").each(function() {
			if ($(this).val() == "") {
				//ifIncludeUnScore = true;
			} else {
				var weight = $(this).data("weight");
				var selfScore = parseFloat($(this).val());
				totalScore += (selfScore / 150) * 150 * (weight / 100);
			}
		});
		$("#selfTotalScoreText").text(totalScore.toFixed(1));
		$("#selfTotalScore").val(totalScore.toFixed(1));
		var level = "E";
		if (totalScore >= 120 && totalScore < 140) {
			level = "B";
		} else if (totalScore >= 140) {
			level = "A";
		}else if(totalScore >= 100 && totalScore < 120)
		{
		   level = "C";
		}else if(totalScore >= 60 && totalScore < 100)
		{
		  level = "D";
		}
		$("#perLevelLb").text(level);
		$("#perLevel").val(level);
	}

	function beforeSubmit(name) {
		getSelfTotalScore();
		ifIncludeUnScore = false;
		$("[id^='self_score_']").each(function() {
			if ($(this).val() == "") {
				ifIncludeUnScore = true;
			}
		});
		if (name != "退回") {
			if (ifIncludeUnScore) { 
				isperp.alert("还有未评价的指标！");
				return false;
			} else if (containUnscore == null) {
				isperp.alert("请先添加考核的指标项！（在绩效模板设定添加）");
				return false;
			} else {
				return true;
			}
		} else {
			return true;
		}
	}
</script>

